package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sign;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.UserVantages;
import cn.wolfcode.trip.base.mapper.SignMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.mapper.UserVantagesMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ISignService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 签到服务
 */
@Service
public class SignServiceImpl implements ISignService {
    @Autowired
    private SignMapper signMapper;
    @Autowired
    private UserVantagesMapper vantagesMapper;

    @Override
    public List<Sign> query(Long userId) {
        List<Sign> signs = signMapper.selectAll(userId);
        return signs;
    }

    @Override
    public Map<String, Object> saveSignTime(Long userId) {
        Map<String , Object> map = new HashMap<>();
        Integer signCount=0;//连续签到次数
        Integer signHistory=0; //表示历史签到
        Date modifyTime=null;//最后签到时间
        Integer vantages=0; //用户表中积分
        String msg=null;

        //获取用户积分
        UserVantages userVantages = vantagesMapper.selectByUserId(userId);

        //判断该用户是否有在积分表的数据
        if(userVantages != null){
            //有用户,获取他的积分数据
            vantages = userVantages.getVantages();
        }else {
            //刚开始有积方,默认为0分
            vantages=0;
        }

        //查询用户的签到表
        List<Sign> signs = signMapper.selectAll(userId);

        if(signs.size() == 0){//没有签到过，第一次签到，执行插入
            msg="欢迎您第一次签到";

            //设置表中的数据
            User user = new User();//用户对象
            Sign sign= new Sign();//签到表对象
            UserVantages u = new UserVantages();//积分表

            signHistory = 1;
            signCount = 1;
            vantages = vantages + 1;
            modifyTime = new Date();//时间精度？timestamp

            //用户的id
            user.setId(userId);

            //签到表的数据的添加
            sign.setModifyTime(modifyTime);
            sign.setUser(user);
            sign.setSignHistory(signHistory);
            sign.setSignCount(signCount);

            //积分的修改
            u.setVantages(vantages);
            u.setUser(user);

            //记录保存

            //保存积分
            if(userVantages != null){
                //有用户
                vantagesMapper.updateByPrimaryKey(u);
            }else {
                vantagesMapper.insert(u);
            }

            //保存第一次签到表
            signMapper.insert(sign);
            map.put("sign" , sign);
        }else{
            //获取前一次的签到数据
            Sign userSign = signs.get(0);
            signHistory = userSign.getSignHistory();
            signCount = userSign.getSignCount();
            modifyTime = userSign.getModifyTime();//上次签到时间


            //计算是否为连续签到
            final long missDays=(DateUtil.getEndDate(new Date()).getTime()-DateUtil.getEndDate(modifyTime).getTime())/(24*60*60*1000);
            System.out.println(missDays);

            if(missDays == 1){//连续签到
                signCount++;
                if (signCount % 7 == 0){
                    Integer v = 5 *(signCount / 7 );
                    if (v >= 50){
                        v = 50;
                    }
                    vantages = vantages + v;
                    msg = "恭喜您已成功连续签到了"+signCount+"天,奖励积分"+v+"分";
                }else {
                    vantages = vantages+ 2 ;
                    msg = "恭喜您已成功连续签到了"+signCount+"天";
                }
            }else{//不连续签到
                signCount = 1;
                vantages = vantages + 1;
                msg="恭喜您已签到成功";
            }

            //签到历史
            modifyTime = new Date();

            Calendar c = Calendar.getInstance();
            c.setTime(modifyTime);
            int i = c.get(Calendar.MONTH) + 1;//当前月份
            c.setTime(userSign.getModifyTime());
            int j = c.get(Calendar.MONTH) + 1;//上一次签到的月份
            if (i != j){
                //判断是否为同一月份,历史记录重新登记
                signHistory = 1;
            }else {
                signHistory = userSign.getSignHistory() + 1;
            }
            //设置表中的数据
            User user = new User();//用户对象
            Sign sign= new Sign();//签到表对象
            UserVantages u = new UserVantages();//积分表

            user.setId(userId);

            sign.setSignCount(signCount);
            sign.setSignHistory(signHistory);
            sign.setUser(user);
            sign.setModifyTime(modifyTime);

            u.setUser(user);
            u.setVantages(vantages);

            //保存积分
            if(userVantages != null){
                //有用户
                vantagesMapper.updateByPrimaryKey(u);
            }else {
                vantagesMapper.insert(u);
            }

            //保存第一次签到表
            signMapper.insert(sign);
            map.put("sign" , sign);
        }
        map.put("msg" , msg);
        return map;
    }
}
